package com.huiquan.medicine.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.huiquan.framework.base.BaseController;
import com.huiquan.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.medicine.service.MedicineService;

/**
 * 药品 controller
 *
 * @author leo
 * @time 2017年4月5日 下午3:20:12
 */
@Controller
@RequestMapping("/medicine")
public class MedicineController extends BaseController {
	@Autowired
	private MedicineService service;

	/**
	 * 查看药品分组列表
	 *
	 * @param tabid
	 * @return
	 */
	@RequestMapping("/medicine_group_list.htm")
	public ModelAndView medicineGroupList(String tabid) {
		Map<String, Object> pageParam;
		if (StringUtil.isNotEmpty(tabid)) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			setSessionValue("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) getSessionValue("pageParam");
		}

		String start = RequestParamUtil.getParamByRequestOrSession(getRequest(), pageParam, "start");
		String keyword = RequestParamUtil.getParamByRequestOrSession(getRequest(), pageParam, "keyword");
		Map<String, Object> map = service.medicineGroupList(start, keyword);
		return new ModelAndView("medicine/medicine_group_list", map);
	}

	/**
	 * 查看疾病药品分组列表
	 *
	 * @param tabid
	 * @return
	 */
	@RequestMapping("/di_medicine_group_list.htm")
	public ModelAndView diMedicineGroupList(String tabid) {
		Map<String, Object> pageParam;
		if (StringUtil.isNotEmpty(tabid)) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			setSessionValue("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) getSessionValue("pageParam");
		}

		String start = RequestParamUtil.getParamByRequestOrSession(getRequest(), pageParam, "start");
		String keyword = RequestParamUtil.getParamByRequestOrSession(getRequest(), pageParam, "keyword");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(getRequest(), pageParam, "countPerPage");

		Map<String, Object> map = service.diMedicineGroupList(start, keyword, countPerPageStr);
		return new ModelAndView("medicine/di_medicine_group_list", map);
	}

	/**
	 * 导入药品分组
	 *
	 * @param file
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("import_medicine_group.do")
	public ReturnData importMedicineGroup(@RequestParam(required = false) MultipartFile file) throws IOException {
		User user = (User) getSessionValue("user");
		return service.importMedicineGroup(user, file);
	}

	/**
	 * 导入疾病药品分组
	 *
	 * @param file
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("import_di_medicine_group.do")
	public ReturnData importDiMedicineGroup(@RequestParam(required = false) MultipartFile file) throws IOException {
		User user = (User) getSessionValue("user");
		return service.importDiMedicineGroup(user, file);
	}

	/**
	 * 更新一条药品分组
	 *
	 * @return
	 */
	@ResponseBody
	@RequestMapping("update_medicine_group.do")
	public ReturnData updateMedicineGroup() {
		User user = (User) getSessionValue("user");
		Map<String, String> params = new HashMap<>();
		params.put("seq", getRequestValue("seq"));
		params.put("groupName", getRequestValue("groupName"));
		params.put("id", getRequestValue("id"));
		params.put("rank", getRequestValue("rank"));
		return service.updateMedicineGroup(params, user);
	}

	/**
	 * 更新一条疾病药品分组
	 *
	 * @return
	 */
	@ResponseBody
	@RequestMapping("update_di_medicine_group.do")
	public ReturnData updateDiMedicineGroup() {
		User user = (User) getSessionValue("user");
		Map<String, String> params = new HashMap<>();
		params.put("seq", getRequestValue("seq"));
		params.put("id", getRequestValue("id"));
		params.put("di", getRequestValue("di"));
		params.put("rank", getRequestValue("rank"));
		params.put("age", getRequestValue("age"));
		params.put("sex", getRequestValue("sex"));
		return service.updateDiMedicineGroup(params, user);
	}

	/**
	 * 删除一条药品分组
	 * 
	 * @param seq
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delete_medicine_group.do")
	public ReturnData deleteMedicineGroup(String seq) {
		return service.deleteMedicineGroup(Integer.valueOf(seq));
	}

	/**
	 * 删除一条疾病药品分组
	 * 
	 * @param seq
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delete_di_medicine_group.do")
	public ReturnData deleteDiMedicineGroup(String seq) {
		return service.deleteDiMedicineGroup(Integer.valueOf(seq));
	}

	/**
	 * 导出药品分组列表
	 *
	 * @param resp
	 * @throws IOException
	 */
	@RequestMapping("export_medicine_group.do")
	public void exportMedicineGroup(HttpServletResponse resp) throws IOException {
		service.exportMedicineGroup(resp);
	}

	/**
	 * 导出疾病药品分组列表
	 *
	 * @param resp
	 * @throws IOException
	 */
	@RequestMapping("export_di_medicine_group.do")
	public void exportDiMedicineGroup(HttpServletResponse resp) throws IOException {
		service.exportDiMedicineGroup(resp);
	}
}
